/**
 * Created by helang.love@qq.com on 2018/3/11.
 */
/*!
 * mobileSelect.js
 * (c) 2017-present onlyhom
 * Released under the MIT License.
 */

window.MobileSelects = (function () {

    function getClass(dom, string) {
        return dom.getElementsByClassName(string);
    }

    //鏋勯€犲櫒
    function MobileSelects(config) {
        this.mobileSelect;
        this.wheelsData = config.wheels;
        this.jsonType = false;
        this.jsonData = [];
        this.checkDataType();
        this.renderWheels(this.wheelsData);
        this.displayJson = [];
        this.cascade = false;
        this.startY;
        this.moveEndY;
        this.moveY;
        this.oldMoveY;
        this.offset = 0;
        this.offsetSum = 0;
        this.oversizeBorder;
        this.curDistance = [];
        this.clickStatus = false;

        this.init(config);
    }

    MobileSelects.prototype = {
        constructor: MobileSelects,
        init: function (config) {
            var _this = this;

            _this.trigger = document.querySelector(config.trigger);
            _this.wheel = getClass(_this.mobileSelect, 'wheel');   //wheel 鏁扮粍
            _this.slider = getClass(_this.mobileSelect, 'selectContainer'); // slider 鏁扮粍
            _this.wheels = _this.mobileSelect.querySelector('.wheels');   //wheels
            _this.liHeight = _this.mobileSelect.querySelector('li').offsetHeight;
            _this.ensureBtn = _this.mobileSelect.querySelector('.ensure');
            _this.closeBtn = _this.mobileSelect.querySelector('.cancel');
            _this.grayLayer = _this.mobileSelect.querySelector('.grayLayer');
            _this.popUp = _this.mobileSelect.querySelector('.content');
            _this.callback = config.callback ? config.callback : function () { };
            _this.transitionEnd = config.transitionEnd ? config.transitionEnd : function () { };
            _this.initPosition = config.position ? config.position : [];
            _this.titleText = config.title ? config.title : '';

            _this.trigger.style.cursor = 'pointer';
            _this.setTitle(_this.titleText);
            _this.checkCascade();

            if (_this.cascade) {
                _this.initCascade();
            }

            //瀹氫綅 鍒濆浣嶇疆
            if (_this.initPosition.length == 0) {
                for (var i = 0; i < _this.slider.length; i++) {
                    _this.initPosition.push(0);
                }
            }

            _this.setCurDistance(_this.initPosition);
            _this.addListenerAll();

            //鎸夐挳鐩戝惉
            _this.closeBtn.addEventListener('click', function () {
                _this.mobileSelect.classList.remove('mobileSelect-show');
            });

            _this.ensureBtn.addEventListener('click', function () {

                _this.mobileSelect.classList.remove('mobileSelect-show');
                //if ($(_this.trigger).hasClass('mutie')) {
                //    _this.mobileSelect.classList.add('mobileSelect-show');
                //    $(_this.trigger).removeClass("mutie")
                //}
                var tempValue = '';
                for (var i = 0; i < _this.wheel.length; i++) {
                    i == _this.wheel.length - 1 ? tempValue += _this.getValue(i) : tempValue += _this.getValue(i) + ' ';
                }
                //_this.trigger.innerHTML = tempValue;
                _this.callback(_this.getIndexArr(), _this.getJson());
            });

            _this.trigger.addEventListener('click', function () {
                _this.mobileSelect.classList.add('mobileSelect-show');
            });
            _this.grayLayer.addEventListener('click', function () {
                _this.mobileSelect.classList.remove('mobileSelect-show');
            });
            _this.popUp.addEventListener('click', function (event) {
                event.stopPropagation();
            });

            _this.fixRowStyle(); //淇鍒楁暟
        },

        setTitle: function (string) {
            var _this = this;
            _this.titleText = string;
            _this.mobileSelect.querySelector('.title').innerHTML = _this.titleText;
        },

        renderWheels: function (wheelsData) {
            var _this = this;
            _this.mobileSelect = document.createElement("div");
            _this.mobileSelect.className = "mobileSelect";
            _this.mobileSelect.innerHTML =
                '<div class="grayLayer"></div>' +
                '<div class="content">' +
                '<div class="btnBar">' +
                '<div class="fixWidth">' +
                '<div class="cancel">取消</div>' +
                '<div class="title"></div>' +
                '<div class="ensure">确定</div>' +
                '</div>' +
                '</div>' +
                '<div class="panel">' +
                '<div class="fixWidth">' +
                '<div class="wheels">' +
                '</div>' +
                '<div class="selectLine"></div>' +
                '<div class="shadowMask"></div>' +
                '</div>' +
                '</div>' +
                '</div>';
            document.body.appendChild(_this.mobileSelect);

            //鏍规嵁鏁版嵁闀垮害鏉ユ覆鏌�

            var tempHTML = '';
            for (var i = 0; i < wheelsData.length; i++) { //鍒�
                tempHTML += '<div class="wheel"><ul class="selectContainer">';
                if (_this.jsonType) {
                    for (var j = 0; j < wheelsData[i].data.length; j++) { //琛�
                        tempHTML += '<li data-id="' + wheelsData[i].data[j].id + '">' + wheelsData[i].data[j].value + '</li>';
                    }
                } else {
                    for (var j = 0; j < wheelsData[i].data.length; j++) { //琛�
                        tempHTML += '<li>' + wheelsData[i].data[j] + '</li>';
                    }
                }
                tempHTML += '</ul></div>';
            }
            _this.mobileSelect.querySelector('.wheels').innerHTML = tempHTML;
        },

        addListenerAll: function () {
            var _this = this;
            for (var i = 0; i < _this.slider.length; i++) {
                //鎵嬪娍鐩戝惉
                (function (i) {
                    _this.addListenerWheel(_this.wheel[i], i);
                    _this.addListenerLi(i);
                })(i);
            }
        },

        addListenerWheel: function (theWheel, index) {
            var _this = this;
            theWheel.addEventListener('touchstart', function (event) {
                _this.touch(event, this.firstChild, index);
            }, false);
            theWheel.addEventListener('touchend', function (event) {
                _this.touch(event, this.firstChild, index);
            }, false);
            theWheel.addEventListener('touchmove', function (event) {
                _this.touch(event, this.firstChild, index);
            }, false);

            //PC鎷栨嫿鐩戝惉
            theWheel.addEventListener('mousedown', function (event) {
                _this.dragClick(event, this.firstChild, index);
            }, false);
            theWheel.addEventListener('mousemove', function (event) {
                _this.dragClick(event, this.firstChild, index);
            }, false);
            theWheel.addEventListener('mouseup', function (event) {
                _this.dragClick(event, this.firstChild, index);
            }, true);
        },

        addListenerLi: function (sliderIndex) {
            var _this = this;
            var curWheelLi = _this.slider[sliderIndex].getElementsByTagName('li');
            for (var j = 0; j < curWheelLi.length; j++) {
                (function (j) {
                    curWheelLi[j].addEventListener('click', function () {
                        _this.singleClick(this, j, sliderIndex);
                    }, false);
                })(j);
            }
        },

        checkDataType: function () {
            var _this = this;
            if (typeof (_this.wheelsData[0].data[0]) == 'object') {
                _this.jsonType = true;
                _this.jsonData = _this.wheelsData[0].data;
            }
        },

        checkCascade: function () {
            var _this = this;
            if (_this.jsonType) {
                var node = _this.wheelsData[0].data;
                for (var i = 0; i < node.length; i++) {
                    if ('childs' in node[i] && node[i].childs.length > 0) {
                        _this.cascade = true;
                        break;
                    }
                }
            } else {
                _this.cascade = false;
            }
        },

        initCascade: function () {
            var _this = this;
            _this.displayJson.push(_this.generateArrData(_this.jsonData));
            _this.checkArrDeep(_this.jsonData[0]);
            //console.log('灏嗚鏄剧ず鐨刯son:'); 
            //console.log(_this.displayJson);
            _this.updateWheels();
        },

        generateArrData: function (targetArr) {
            var tempArr = [];
            for (var i = 0; i < targetArr.length; i++) {
                tempArr.push({
                    "id": targetArr[i].id,
                    "value": targetArr[i].value
                });
            }
            return tempArr;
        },

        checkArrDeep: function (parent) { //妫€娴嬪瓙鑺傜偣娣卞害  淇敼 displayJson
            var _this = this;
            if ('childs' in parent && parent.childs.length > 0) {
                _this.displayJson.push(_this.generateArrData(parent.childs)); //鐢熸垚瀛愯妭鐐规暟缁�
                _this.checkArrDeep(parent.childs[0]);//妫€娴嬩笅涓€涓瓙鑺傜偣
            }
        },

        checkRange: function (index, posIndexArr) {
            var _this = this;
            var deleteNum = _this.displayJson.length - 1 - index;
            for (var i = 0; i < deleteNum; i++) {
                _this.displayJson.pop();
            }
            var resultNode;
            for (var i = 0; i <= index; i++) {
                if (i == 0)
                    resultNode = _this.jsonData[posIndexArr[0]];
                else {
                    resultNode = resultNode.childs[posIndexArr[i]];
                }
            }
            _this.checkArrDeep(resultNode);
            //console.log(_this.displayJson);
            _this.updateWheels();
            _this.fixRowStyle();
            _this.setCurDistance(_this.resetPostion(index, posIndexArr));
        },

        resetPostion: function (index, posIndexArr) {
            var _this = this;
            var tempPosArr = posIndexArr;
            var tempCount;
            if (_this.slider.length > posIndexArr.length) {
                tempCount = _this.slider.length - posIndexArr.length;
                for (var i = 0; i < tempCount; i++) {
                    tempPosArr.push(0);
                }
            } else if (_this.slider.length < posIndexArr.length) {
                tempCount = posIndexArr.length - _this.slider.length;
                for (var i = 0; i < tempCount; i++) {
                    tempPosArr.pop();
                }
            }
            for (var i = index + 1; i < tempPosArr.length; i++) {
                tempPosArr[i] = 0;
            }
            return tempPosArr;
        },

        updateWheels: function () {
            var _this = this;
            if (_this.wheel.length > _this.displayJson.length) {
                var count = _this.wheel.length - _this.displayJson.length;
                for (var i = 0; i < count; i++) {
                    _this.wheels.removeChild(_this.wheel[_this.wheel.length - 1]);
                }
            }

            for (var i = 0; i < _this.displayJson.length; i++) {
                (function (i) {
                    var tempHTML = '';
                    if (_this.wheel[i]) {
                        for (var j = 0; j < _this.displayJson[i].length; j++) {
                            tempHTML += '<li data-id="' + _this.displayJson[i][j].id + '">' + _this.displayJson[i][j].value + '</li>';
                        }
                        _this.slider[i].innerHTML = tempHTML;

                    } else {
                        var tempWheel = document.createElement("div");
                        tempWheel.className = "wheel";
                        tempHTML = '<ul class="selectContainer">';
                        for (var j = 0; j < _this.displayJson[i].length; j++) {
                            tempHTML += '<li data-id="' + _this.displayJson[i][j].id + '">' + _this.displayJson[i][j].value + '</li>';
                        }
                        tempHTML += '</ul>';
                        tempWheel.innerHTML = tempHTML;

                        _this.addListenerWheel(tempWheel, i);
                        _this.wheels.appendChild(tempWheel);
                    }
                    _this.addListenerLi(i);
                })(i);
            }
        },

        updateWheel: function (sliderIndex, data) {
            var _this = this;
            var tempHTML = '';
            for (var j = 0; j < data.length; j++) {
                tempHTML += '<li>' + data[j] + '</li>'
            }
            _this.slider[sliderIndex].innerHTML = tempHTML;
            _this.addListenerLi(sliderIndex);
        },

        fixRowStyle: function () {
            var _this = this;
            var width = (100 / _this.wheel.length).toFixed(2);
            for (var i = 0; i < _this.wheel.length; i++) {
                _this.wheel[i].style.width = width + '%';
            }
        },

        getIndex: function (distance) {
            return Math.round((2 * this.liHeight - distance) / this.liHeight);
        },

        getIndexArr: function () {
            var _this = this;
            var temp = [];
            for (var i = 0; i < _this.curDistance.length; i++) {
                temp.push(_this.getIndex(_this.curDistance[i]));
            }
            return temp;
        },

        getJson: function () {
            var _this = this;
            var temp = [];
            var positionArr = _this.getIndexArr();
            if (_this.cascade) {
                for (var i = 0; i < _this.wheel.length; i++) {
                    temp.push(_this.displayJson[i][positionArr[i]]);
                }
            }
            else if (_this.jsonType) {
                for (var i = 0; i < _this.curDistance.length; i++) {
                    temp.push(_this.wheelsData[i].data[_this.getIndex(_this.curDistance[i])]);
                }
            } else {
                for (var i = 0; i < _this.curDistance.length; i++) {
                    temp.push(_this.getValue(i));
                }
            }
            return temp;
        },

        calcDistance: function (index) {
            return 2 * this.liHeight - index * this.liHeight;
        },

        setCurDistance: function (indexArr) {
            var _this = this;
            var temp = [];
            for (var i = 0; i < _this.slider.length; i++) {
                temp.push(_this.calcDistance(indexArr[i]));
                _this.movePosition(_this.slider[i], temp[i]);
            }
            _this.curDistance = temp;
        },

        fixPosition: function (distance) {
            return -(this.getIndex(distance) - 2) * this.liHeight;
        },

        movePosition: function (theSlider, distance) {
            theSlider.style.webkitTransform = 'translate3d(0,' + distance + 'px, 0)';
            theSlider.style.transform = 'translate3d(0,' + distance + 'px, 0)';
        },

        locatePostion: function (index, posIndex) {
            this.curDistance[index] = this.calcDistance(posIndex);
            this.movePosition(this.slider[index], this.curDistance[index]);
        },

        updateCurDistance: function (theSlider, index) {
            this.curDistance[index] = parseInt(theSlider.style.transform.split(',')[1]);
        },

        getDistance: function (theSlider) {
            return parseInt(theSlider.style.transform.split(',')[1]);
        },

        getValue: function (sliderIndex) {
            var _this = this;
            var index = _this.getIndex(_this.curDistance[sliderIndex]);
            return _this.slider[sliderIndex].getElementsByTagName('li')[index].innerHTML;
        },

        touch: function (event, theSlider, index) {
            var _this = this;
            event = event || window.event;
            switch (event.type) {
                case "touchstart":
                    _this.startY = event.touches[0].clientY;
                    _this.oldMoveY = _this.startY;
                    break;

                case "touchend":

                    _this.moveEndY = event.changedTouches[0].clientY;
                    _this.offsetSum = _this.moveEndY - _this.startY;

                    //淇浣嶇疆
                    _this.updateCurDistance(theSlider, index);
                    _this.curDistance[index] = _this.fixPosition(_this.curDistance[index]);
                    _this.movePosition(theSlider, _this.curDistance[index]);
                    _this.oversizeBorder = -(theSlider.getElementsByTagName('li').length - 3) * _this.liHeight;


                    //鍙嶅脊
                    if (_this.curDistance[index] + _this.offsetSum > 2 * _this.liHeight) {
                        _this.curDistance[index] = 2 * _this.liHeight;
                        setTimeout(function () {
                            _this.movePosition(theSlider, _this.curDistance[index]);
                        }, 100);

                    } else if (_this.curDistance[index] + _this.offsetSum < _this.oversizeBorder) {
                        _this.curDistance[index] = _this.oversizeBorder;
                        setTimeout(function () {
                            _this.movePosition(theSlider, _this.curDistance[index]);
                        }, 100);
                    }


                    _this.transitionEnd(_this.getIndexArr(), _this.getJson());

                    if (_this.cascade) {
                        var tempPosArr = _this.getIndexArr();
                        tempPosArr[index] = _this.getIndex(_this.curDistance[index]);
                        _this.checkRange(index, tempPosArr);
                    }

                    break;

                case "touchmove":
                    event.preventDefault();
                    _this.moveY = event.touches[0].clientY;
                    _this.offset = _this.moveY - _this.oldMoveY;

                    _this.updateCurDistance(theSlider, index);
                    _this.curDistance[index] = _this.curDistance[index] + _this.offset;
                    _this.movePosition(theSlider, _this.curDistance[index]);
                    _this.oldMoveY = _this.moveY;
                    break;
            }
        },

        dragClick: function (event, theSlider, index) {
            var _this = this;
            event = event || window.event;
            switch (event.type) {
                case "mousedown":
                    _this.startY = event.clientY;
                    _this.oldMoveY = _this.startY;
                    _this.clickStatus = true;
                    break;

                case "mouseup":

                    _this.moveEndY = event.clientY;
                    _this.offsetSum = _this.moveEndY - _this.startY;

                    //淇浣嶇疆
                    _this.updateCurDistance(theSlider, index);
                    _this.curDistance[index] = _this.fixPosition(_this.curDistance[index]);
                    _this.movePosition(theSlider, _this.curDistance[index]);
                    _this.oversizeBorder = -(theSlider.getElementsByTagName('li').length - 3) * _this.liHeight;


                    //鍙嶅脊
                    if (_this.curDistance[index] + _this.offsetSum > 2 * _this.liHeight) {
                        _this.curDistance[index] = 2 * _this.liHeight;
                        setTimeout(function () {
                            _this.movePosition(theSlider, _this.curDistance[index]);
                        }, 100);

                    } else if (_this.curDistance[index] + _this.offsetSum < _this.oversizeBorder) {
                        _this.curDistance[index] = _this.oversizeBorder;
                        setTimeout(function () {
                            _this.movePosition(theSlider, _this.curDistance[index]);
                        }, 100);
                    }

                    _this.clickStatus = false;
                    _this.transitionEnd(_this.getIndexArr(), _this.getJson());
                    if (_this.cascade) {
                        var tempPosArr = _this.getIndexArr();
                        tempPosArr[index] = _this.getIndex(_this.curDistance[index]);
                        _this.checkRange(index, tempPosArr);
                    }
                    break;

                case "mousemove":
                    event.preventDefault();
                    if (_this.clickStatus) {
                        _this.moveY = event.clientY;
                        _this.offset = _this.moveY - _this.oldMoveY;

                        _this.updateCurDistance(theSlider, index);
                        _this.curDistance[index] = _this.curDistance[index] + _this.offset;
                        _this.movePosition(theSlider, _this.curDistance[index]);
                        _this.oldMoveY = _this.moveY;
                    }
                    break;
            }
        },

        singleClick: function (theLi, index, sliderIndex) {
            var _this = this;
            if (_this.cascade) {
                var tempPosArr = _this.getIndexArr();
                tempPosArr[sliderIndex] = index;
                _this.checkRange(sliderIndex, tempPosArr);

            } else {
                _this.curDistance[sliderIndex] = (2 - index) * _this.liHeight;
                _this.movePosition(theLi.parentNode, _this.curDistance[sliderIndex]);
            }
        }

    };

    return MobileSelects;
})();
$.extend({
    selectDates: function (g, a, k) {
        var d, e, b;
        b = new Date;
        !a.start || !a.end || a.end < a.start ? (d = b.getFullYear() - 60, e = b.getFullYear()) : (d = a.start, e = a.end);
        b = a.select && 3 == a.select.length ? [a.select[0] - d, a.select[1] - 1, a.select[2] - 1] : [e - d, b.getMonth(), b.getDate() - 1];
        new MobileSelects({
            trigger: g,
            title: a.title || "\u624b\u52bf\u62d6\u52a8\u9009\u62e9\u65e5\u671f",
            wheels: function (a, b) {
                for (var d = [{
                    data: []
                }]; a <= b; a++) {
                    for (var e = {
                        id: a,
                        value: a + "\u5e74",
                        childs: []
                    }, c = 1; 12 >= c; c++) {
                        for (var h = [], g = 1 == c || 3 == c || 5 == c || 7 == c || 8 == c || 10 == c || 12 == c ? 31 : 2 == c ? 0 == a % 4 && 0 != a % 100 ? 29 : 0 == a % 400 ? 29 : 28 : 30, f = 1; f <= g; f++) h.push({
                            id: f,
                            value: f + "\u65e5"
                        });
                        e.childs.push({
                            id: c,
                            value: c + "\u6708",
                            childs: h
                        })
                    }
                    d[0].data.push(e)
                }
                return d
            }(d, e),
            position: b,
            callback: function (a, b) {
                k({
                    year: b[0].id,
                    month: b[1].id,
                    day: b[2].id
                })
            }
        })
    }
});